package main

import "fmt"

func main() {
	//map1 := map[int]int{1: 1, 2: 2, 3: 3, 10: 10}
	//modify(map1)
	//fmt.Println(map1)

	//mapStruct()
	mapExercise()
}

func modify(m map[int]int) {
	m[10] = 100
}

// Stu 定义一个学生结构体
type Stu struct {
	name string
	age  int
	addr string
}

func mapStruct() {
	students := make(map[string]Stu, 10)
	stu1 := Stu{name: "tom", age: 16, addr: "上海"}
	stu2 := Stu{name: "jack", age: 16, addr: "北京"}
	stu3 := Stu{name: "bill", age: 16, addr: "山东"}
	stu4 := Stu{name: "kathy", age: 16, addr: "天津"}
	students["no1"] = stu1
	students["no2"] = stu2
	students["no3"] = stu3
	students["no4"] = stu4
	fmt.Println(students)

	// 遍历
	for k, v := range students {
		fmt.Printf("学生的编号是%v\n", k)
		fmt.Printf("学生的名称是%v\n", v.name)
		fmt.Printf("学生的年龄是%v\n", v.age)
		fmt.Printf("学生的地址是%v\n", v.addr)
		fmt.Println()
	}
}

func mapExercise() {
	users := make(map[string]map[string]string)
	users["tom"] = map[string]string{
		"nickName": "tomNotTome",
		"password": "213545345",
	}
	modifyUser(users, "tom", "tomNotTome", "654557")
	fmt.Println(users)
	modifyUser(users, "Jack", "JJJJJack", "235123456")
	fmt.Println(users)
}

func modifyUser(users map[string]map[string]string, name string, nickName string, pwd string) {
	if users[name] != nil {
		users[name]["password"] = "88888888"

	} else {
		users[name] = make(map[string]string, 2)
		users[name]["nickName"] = nickName
		users[name]["password"] = pwd
	}
}
